package com.kgd.news.wxpay.util;

import com.alibaba.fastjson.JSONObject;
import com.kgd.news.entity.AutoWithdraw;
import com.kgd.news.entity.WithdrawSystemerror;
import com.kgd.news.utils.Api;
import com.kgd.news.wxpay.constant.Constant;
import org.apache.log4j.Logger;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

public class WxPayCheckUtil {
    private static Logger log = Logger.getLogger(WxPayCheckUtil.class);
    /**
     * 微信支付每天有限额时间吗，这样写的代码可读性太差了
     * 每人每天只能提现10次。
     */
    public static void checkUnPayOrders() {
        List<AutoWithdraw> autoWithdrawList = AutoWithdraw.dao.find("select * from auto_withdraw where state = " + Constant.AUTO_WITHDRAW_FAIL);
        for (AutoWithdraw autoWithdraw : autoWithdrawList
                ) {
            synchronized (RedPacketSplitUtil.class) {
                String userid = autoWithdraw.getUserid();
                int withDrawFee = autoWithdraw.getWithdrawFee();
                int cashWithdraw = autoWithdraw.getCashwithdraw();
                InetAddress address = null;
                try {
                    address = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    log.error("InetAddress.getLocalHost()错误");
                    e.printStackTrace();
                }
                log.error("address=" + address);
                String addressIp = address.getHostAddress();
                if (cashWithdraw > 0 && (cashWithdraw == withDrawFee)) {
//                    这个不是自我安慰吗，state一个可以确保正确了
                    autoWithdraw.setState(Constant.AUTO_WITHDRAW_SUCCESS);
                    autoWithdraw.update();
                } else {
                    JSONObject jsonObject = RedPacketSplitUtil.withdrawCashAuto(addressIp, userid, withDrawFee, true);
                    if ((int) jsonObject.get("code") == Api.S_200) {
//                        这块中文是否有潜在的风险呢,只用这个票判断太单一了，
//                        咋么防止出现多次呢
                        log.error("jsonmsg=" + jsonObject.getString("msg"));
                        if (!"余额不足，账户余额已扣除，7天内自动到账".equals(jsonObject.getString("msg"))) {
                            log.error("自动提现成功，userid=" + userid + "withDrawFee=" + withDrawFee);
                            autoWithdraw.setState(Constant.AUTO_WITHDRAW_SUCCESS);
                            autoWithdraw.setWithdrawtime(System.currentTimeMillis() + "");
                            autoWithdraw.setCashwithdraw(withDrawFee);
                            autoWithdraw.update();
                            log.error("更新成功");
                        } else {
                            log.error("余额不足，账户余额已扣除，7天内自动到账");
                        }
                    } else {
                        log.error("withdrawCashAuto返回不是200");
                    }
                }
            }
        }
    }

    /**
     *检查系统错误的微信提现订单，查看真实的执行结果
     */
    public static void checkWithdrawSystemError() {
        List<WithdrawSystemerror> withdrawSystemerrorList = WithdrawSystemerror.dao.find("select * from withdraw_systemerror where state =0");
        for (WithdrawSystemerror bean : withdrawSystemerrorList
                ) {
            int moneyNum = bean.getMoneyNum();
//            查询只需要商户订单号吧
            String partnerTradeN0 = bean.getPartnerTradeNo();
            check0rderPayQuery(partnerTradeN0);
        }
    }

    public static void check0rderPayQuery(String partnerTradeN0) {
//        TransferController.orderPayQuery(partnerTradeN0);
    }


    public static void addWxPaySystemError(int moneyNum, String partner_trade_no) {
        WithdrawSystemerror withdrawSystemerror = new WithdrawSystemerror();
        withdrawSystemerror.setMoneyNum(moneyNum);
        withdrawSystemerror.setPartnerTradeNo(partner_trade_no);
        withdrawSystemerror.setState(Constant.WITHDRAW_SYSTEMERROR_QUERY_SYSTEMERROR);
        withdrawSystemerror.save();
    }
}
